<script setup>
import { ref } from 'vue'
const passWordInfo = ref({
    old_pwd:'',
    new_pwd:'',
    re_pwd:''
})
const rules = {
    old_pwd: [
        { required: true, message: '请输入原密码', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '密码必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    new_pwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '密码必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    re_pwd:[
        { required: true, message: '再次确认新密码', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '密码必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ]
}
import { updateUserPasswordService } from '../../api/user'
import { ElMessage } from 'element-plus';
import useUserInfoStore from '@/store/userInfo'
const userInfoStore = useUserInfoStore()
import router from '@/router';
const updateUserInfo = async() => {
    let res=await updateUserPasswordService(passWordInfo.value)
    //修改成功
    ElMessage.success(res.message?res.message:'修改成功')
    //跳转到登录页，并清除pinia中的用户信息
    userInfoStore.removeUserInfo()
    router.push('/')    
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span></span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="passWordInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="old_pwd">
                        <el-input v-model="passWordInfo.old_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input v-model="passWordInfo.new_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="re_pwd">
                        <el-input v-model="passWordInfo.re_pwd"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">修改密码</el-button>
                        
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>